﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using KPIS.GERP.WEB.GFMS.Class;
using KPIS.GERP.WEB.GFMS.Class;

namespace KPIS.GERP.HtmlHelpers
{
    /// <summary>
    /// Target = <input type="button" onclick="window.location.href='url_link'" value="link_text" />
    /// </summary>
    public static class ButtonLinkHelper
    {

        public static IHtmlString ButtonAdd(this HtmlHelper helper, bool isMini = false
                                            , string actionName = "Create", string id = "")
        {
            var currentUrl = HttpContext.Current.Request.RawUrl;
            //var currentUrlSubStr = currentUrl.Split('/');
            //var controllerName = currentUrlSubStr.ElementAtOrDefault(1);

            return ButtonLink(helper, "เพิ่ม", actionName, GimsUtilities.ControllerName(helper), "btn-success", "icon-plus"
                              , (isMini ? "btn-mini" : "button-standard-width btn-small")
                              , alternateText: "เพิ่มข้อมูล", id: id);
        }


        public static IHtmlString ButtonEdit(this HtmlHelper helper, object routeValues, bool isMini = false
                                             , string actionName = "Edit", string id = "")
        {
            var currentUrl = HttpContext.Current.Request.RawUrl;
            var currentUrlSubStr = currentUrl.Split('/');
            //var controllerName = currentUrlSubStr.ElementAtOrDefault(1);

            return ButtonLink(helper, "แก้ไข", actionName, GimsUtilities.ControllerName(helper), "btn-success", "icon-edit"
                              , (isMini ? "btn-mini" : "button-standard-width btn-small")
                              , alternateText: "แก้ไขข้อมูล", routeValues: routeValues, id: id);
        }

        public static IHtmlString ButtonDelete(this HtmlHelper helper, object routeValues, bool isMini = false
                                               , string actionName = "Delete", string id = "")
        {
            var currentUrl = HttpContext.Current.Request.RawUrl;
            var currentUrlSubStr = currentUrl.Split('/');
            //var controllerName = currentUrlSubStr.ElementAtOrDefault(1);

            return ButtonLink(helper, "ลบ", actionName, GimsUtilities.ControllerName(helper), "btn-danger", "icon-trash"
                              , (isMini ? "btn-mini" : "button-standard-width btn-small")
                              , alternateText: "ลบข้อมูล", routeValues: routeValues, submitString: "ต้องการลบรายการที่เลือกไว้หรือไม่?", id: id);
        }

        public static IHtmlString ButtonDeleteAll(this HtmlHelper helper, bool isMini = false)
        {
            var currentUrl = HttpContext.Current.Request.RawUrl;
            var currentUrlSubStr = currentUrl.Split('/');
            //var controllerName = currentUrlSubStr.ElementAtOrDefault(1);

            return ButtonLinkSubmit(helper, "ลบที่เลือก", "btn-danger", "icon-trash"
                                    , (isMini ? "btn-mini" : "button-standard-width btn-small")
                                    , alternateText: "ลบข้อมูลที่เลือก");
        }

        public static IHtmlString ButtonSubmitAll(this HtmlHelper helper, bool isMini = false)
        {
            var currentUrl = HttpContext.Current.Request.RawUrl;
            var currentUrlSubStr = currentUrl.Split('/');
            //var controllerName = currentUrlSubStr.ElementAtOrDefault(1);

            return ButtonLinkSubmit(helper, "สร้างคำขอ", "btn-success", "icon-plus"
                                    , (isMini ? "btn-mini" : "button-standard-width btn-small")
                                    , alternateText: "ยืนยันข้อมูลที่เลือก");
        }

        public static IHtmlString ButtonCancel(this HtmlHelper helper, object routeValues, bool isMini = false
                                               , string actionName = "Details", string id = "")
        {
            var currentUrl = HttpContext.Current.Request.RawUrl;
            var currentUrlSubStr = currentUrl.Split('/');
            //var controllerName = currentUrlSubStr.ElementAtOrDefault(1);

            return ButtonLink(helper, "ยกเลิก", actionName, GimsUtilities.ControllerName(helper), "btn-danger", "icon-remove"
                              , (isMini ? "btn-mini" : "button-standard-width btn-small")
                              , alternateText: "ยกเลิกการแก้ไข", routeValues: routeValues, id: id);
        }

        public static IHtmlString ButtonSubmit(this HtmlHelper helper, bool isMini = false)
        {
            var currentUrl = HttpContext.Current.Request.RawUrl;
            var currentUrlSubStr = currentUrl.Split('/');
            //var actionName = currentUrlSubStr.ElementAtOrDefault(2);

            var actionName = string.IsNullOrWhiteSpace(GimsUtilities.ActionName(helper)) || GimsUtilities.ActionName(helper).IndexOf('?') == -1 ? GimsUtilities.ActionName(helper) : GimsUtilities.ActionName(helper).Substring(0, GimsUtilities.ActionName(helper).IndexOf('?'));
            return ButtonSubmit(helper, actionName, isMini);
        }

        public static IHtmlString ButtonSubmit(this HtmlHelper helper, string actionName, bool isMini = false)
        {
            var currentUrl = HttpContext.Current.Request.RawUrl;
            var currentUrlSubStr = currentUrl.Split('/');
            //var controllerName = currentUrlSubStr.ElementAtOrDefault(1);

            return ButtonLinkSubmit(helper, "บันทึก", "btn-success", "icon-ok"
                                    , (isMini ? "btn-mini" : "button-standard-width btn-small")
                                    , alternateText: "บันทึกข้อมูล");
        }

        public static IHtmlString ButtonViewAll(this HtmlHelper helper, bool isMini = false
                                                , string actionName = "Index", string id = "")
        {
            var currentUrl = HttpContext.Current.Request.RawUrl;
            var currentUrlSubStr = currentUrl.Split('/');
            //var controllerName = currentUrlSubStr.ElementAtOrDefault(1);

            return ButtonLink(helper, "กลับไปหน้าแสดงข้อมูลทั้งหมด", actionName, GimsUtilities.ControllerName(helper), "btn-gray", "icon-reply"
                              , (isMini ? "btn-mini" : "button-standard-width btn-small")
                              , alternateText: "กลับไปหน้าแสดงข้อมูลทั้งหมด", id: id);
        }

        public static IHtmlString ButtonDetails(this HtmlHelper helper, object routeValues, bool isMini = false
                                                , string actionName = "Details", string id = "")
        {
            var currentUrl = HttpContext.Current.Request.RawUrl;
            var currentUrlSubStr = currentUrl.Split('/');
            //var controllerName = currentUrlSubStr.ElementAtOrDefault(1);

            return ButtonLink(helper, "ดูรายละเอียด", actionName, GimsUtilities.ControllerName(helper), "btn-gray", "icon-search"
                              , (isMini ? "btn-mini" : "button-standard-width btn-small")
                              , alternateText: "ดูรายละเอียด", routeValues: routeValues, id: id);
        }

        public static IHtmlString ButtonLink(this HtmlHelper helper, string text, string actionName = null, string controllerName = null,
                                             string colorClass = "btn-primary", string iconClass = null, string sizeClass = "button-standard-width btn-small",
                                             string alternateText = null, object routeValues = null, string id = "", string submitString = "")
        {
            var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);

            var currentUrl = HttpContext.Current.Request.RawUrl;
            var currentUrlSubStr = currentUrl.Split('/');
            //controllerName = controllerName?? currentUrlSubStr.ElementAtOrDefault(1);
            //actionName = actionName??currentUrlSubStr.ElementAtOrDefault(2);

            controllerName = controllerName ?? GimsUtilities.ControllerName(helper);
            actionName = actionName ?? GimsUtilities.ActionName(helper);


            var url = string.IsNullOrWhiteSpace(controllerName)
                          ? urlHelper.Action(actionName, routeValues)
                          : urlHelper.Action(actionName, controllerName, routeValues);

            // Create button
            var buttonTagBuilder = new TagBuilder("a");
            buttonTagBuilder.MergeAttribute("role", "button");
            buttonTagBuilder.MergeAttribute("class", string.Format("btn {0} {1}", sizeClass, colorClass));
            buttonTagBuilder.MergeAttribute("data-rel", "tooltip");
            buttonTagBuilder.MergeAttribute("href", "#");

            if (id.Length > 0)
            {
                buttonTagBuilder.MergeAttribute("id", id);
            }

            if (!string.IsNullOrWhiteSpace(submitString))
            {

                //buttonTagBuilder.MergeAttribute("onclick", string.Format("return bootbox.confirm('{0}');", submitString));
                //buttonTagBuilder.MergeAttribute("onclick", string.Format("if (bootbox.confirm('{0}')) return window.location.href='{1}'; else return false;", submitString, url));
                buttonTagBuilder.MergeAttribute("onclick", string.Format("bootbox.confirm('{0}', function(result) {{ if (result) window.location.href='{1}'; }});", submitString, url));
            }
            else
            {
                buttonTagBuilder.MergeAttribute("onclick", string.Format("window.location.href='{0}'", url));
            }

            // Create i
            var iTagBuilder = new TagBuilder("i");
            if (!string.IsNullOrWhiteSpace(iconClass)) iTagBuilder.MergeAttribute("class", iconClass + " bigger-120");
            buttonTagBuilder.InnerHtml = iTagBuilder.ToString();
            buttonTagBuilder.InnerHtml += (sizeClass == "button-standard-width btn-small" ? "&nbsp;&nbsp;" + text : "");

            return helper.Raw(buttonTagBuilder.ToString());
        }

        public static IHtmlString ButtonLinkSubmit(this HtmlHelper helper, string text,
                                                   string colorClass = "btn-primary", string iconClass = null, string sizeClass = "button-standard-width btn-small",
                                                   string alternateText = null)
        {
            //var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);

            //var currentUrl = HttpContext.Current.Request.RawUrl;
            //var currentUrlSubStr = currentUrl.Split('/');
            //controllerName = controllerName ?? currentUrlSubStr.ElementAtOrDefault(1);
            //actionName = actionName ?? currentUrlSubStr.ElementAtOrDefault(2);

            //var url = string.IsNullOrWhiteSpace(controllerName)
            //              ? urlHelper.Action(actionName, routeValues)
            //              : urlHelper.Action(actionName, controllerName, routeValues);

            // Create button
            var buttonTagBuilder = new TagBuilder("button");
            buttonTagBuilder.MergeAttribute("class", string.Format("btn {0} {1}", sizeClass, colorClass));
            buttonTagBuilder.MergeAttribute("type", "submit");

            // Create i
            var iTagBuilder = new TagBuilder("i");
            if (!string.IsNullOrWhiteSpace(iconClass)) iTagBuilder.MergeAttribute("class", iconClass + " bigger-120");
            buttonTagBuilder.InnerHtml = iTagBuilder.ToString();
            buttonTagBuilder.InnerHtml += (sizeClass == "button-standard-width btn-small" ? "&nbsp;&nbsp;" + text : "");

            return helper.Raw(buttonTagBuilder.ToString());
        }
    }
}